package com.walle.concurrenttest.pattern.twoparsetermination;

/**
 * @author zhangjiapeng
 * @Package com.walle.concurrenttest.pattern.twoparsetermination
 * @Description: ${todo}
 * @date 2019/2/26 14:41
 */
public class CountUpThread extends Thread {

    private int counter;

    private volatile boolean shutdown = false;

    @Override
    public void run() {
        try {
            while (!shutdown){
                doWork();
            }
        } catch (InterruptedException e) {
        } finally {
          doShutdown();
        }
    }

    public void shutdown(){
        this.shutdown = true;
        this.interrupt();
    }

    public boolean isShutdown() {
        return shutdown;
    }

    private void doWork() throws InterruptedException {
        System.out.println("do work: counter = " + counter );
        counter++;
        Thread.sleep(1000);
    }


    private void doShutdown(){
        System.out.println("do shutdown: counter = " + counter);
    }
}
